package models

import (
	"database/sql/driver"
	"encoding/json"
	promentheus "go.didapinche.com/foundation/plat-echo/pkg/prometheus"
	"go.didapinche.com/time"
)

const AlertCurrentTableName = "t_alert_current"

type Alerts []*promentheus.Alert
type AlertCurrent struct {
	ID             uint64       `gorm:"column:id" json:"id"`
	Name           string       `gorm:"column:name" json:"name"`
	AppID          uint64       `gorm:"column:app_id" json:"app_id"`
	HostID         uint64       `gorm:"column:host_id" json:"host_id"`
	DashboardID    uint64       `gorm:"column:dashboard_id" json:"dashboard_id"`
	GraphID        uint64       `gorm:"column:graph_id" json:"graph_id"`
	GraphName      string       `gorm:"column:graph_name" json:"graph_name"`
	AlertName      string       `gorm:"column:alert_name" json:"alert_name"`
	Threshold      string       `gorm:"column:threshold" json:"threshold"`
	GraphType      string       `gorm:"column:graph_type" json:"graph_type"`
	Alerts         *Alerts      `gorm:"type:json;column:alerts" json:"alerts"`
	AlertLevel     int          `gorm:"column:alert_level" json:"alert_level"`
	ClaimStatus    int          `gorm:"column:claim_status" json:"claim_status"`
	ClaimUser      string       `gorm:"column:claim_user" json:"claim_user"`
	StartAlertTime time.Time    `gorm:"column:start_alert_time" json:"start_alert_time"`
	DevGroupID     string       `gorm:"column:dev_group_id" json:"dev_group_id,omitempty" form:"dev_group_id"`
	Duration       string       `gorm:"column:duration" json:"duration"`
	ApiID          uint64       `gorm:"column:api_id" json:"api_id"`
	Owner          string       `gorm:"column:owner" json:"owner,omitempty" form:"owner"`
	Level          int          `gorm:"column:level" json:"level"`
	App            string       `gorm:"-" json:"app"`
	ApiInfo        *Api         `gorm:"-" json:"api_info"`
	AppInfo        *Application `gorm:"-" json:"-"`
	TemplateType   int          `gorm:"-" json:"template_type"` //1:http 2:zeus
	Format         UnitType     `gorm:"-"  json:"format"`
	Owners         []string     `gorm:"-"  json:"owners"`
	AppName        string       `gorm:"-" json:"app_name"`
	ApiName        string       `gorm:"-" json:"api_name"`
	Token          string       `gorm:"-" json:"token"`
}

func (AlertCurrent) TableName() string {
	return AlertCurrentTableName
}

func (a *Alerts) Value() (driver.Value, error) {
	return json.Marshal(a)
}

func (a *Alerts) Scan(input interface{}) error {
	return json.Unmarshal(input.([]byte), a)
}
